﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;
using System.Text;

namespace HY.WEB.SA
{
    public partial class zjpubview : PageBase
    {

        public string whe = "";
        public string us,um,ubh;
        public string tab = "zj";

        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();
            
            if (ViewState["tab"] != null)
            {
                if (ViewState["tab"].ToString() != "")
                    tab = ViewState["tab"].ToString();

            }
            else
                tab = "zj";

            if (!IsPostBack)
            {

                if (tab == "") tab = "zj";
             btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
             //   btnNew.OnClientClick = Window1.GetSaveStateReference(hidstype.ClientID)
//+Window1.GetShowReference("work_add.aspx?stype=" + stype + "", "新增");
                 btnSearch.OnClientClick =  Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
      
                BindGrid();
                fill(); //填充查询
                
            }

        }
          private void fill()
          {
             DataTable d=BLL.BLLfun.get_search(tab);
             for (int i = 0; i < d.Rows.Count; i++)
                    {
                        ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
                    }
               ddlfiled.DataBind();

               DataTable e = BLL.BLLfun.get_where();
               for (int i = 0; i < e.Rows.Count; i++)
               {
                   ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
               }
               ddlwhere.DataBind();

            
        }
          //检查是否有图标
          public static string checkico(string val)
          {
              DataTable dte = BLL.Bllbase.get_table("[company_fav]", "cbh='" + val + "'");
              if (dte.Rows.Count > 0)
                  return "../icon/lock.png";
              else
                  return "../icon/user.png";

          }
        private void BindGrid()
        {
            DataTable table;
            

            table = BLL.Bllbase.get_table(tab,whe);
         

            Grid1.DataSource = table;
            Grid1.DataBind();

        }

        void hidd_void()
        {

            GridColumn personColumn = Grid1.FindColumn("ct2");
            personColumn.Hidden = true;

            GridColumn jobColumn = Grid1.FindColumn("ct3");
            jobColumn.Hidden = true;

            GridColumn ctelColumn = Grid1.FindColumn("ct4");
            ctelColumn.Hidden = true;

            GridColumn mtelColumn = Grid1.FindColumn("ct5");
            mtelColumn.Hidden = true;
        }


        void show_void()
        {

            GridColumn personColumn = Grid1.FindColumn("ct2");
            personColumn.Hidden = false;

            GridColumn jobColumn = Grid1.FindColumn("ct3");
            jobColumn.Hidden = false;

            GridColumn ctelColumn = Grid1.FindColumn("ct4");
            ctelColumn.Hidden = false;

            GridColumn mtelColumn = Grid1.FindColumn("ct5");
            mtelColumn.Hidden = false;


        }
        protected void btnAll_Click(object sender, EventArgs e)
        {

            ViewState["tab"] = "zj";
            tab = "zj";

            Grid1.Title = "所有信息";
            BindGrid();

        }

        protected void btnno_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_zj_pub";
            tab = "vi_zj_pub";

            Grid1.Title = "公共信息";
            BindGrid();
            show_void();
        }

        protected void btnyes_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_zj_self";
            tab = "vi_zj_self";
            Grid1.Title = "个人信息";

            BindGrid(); hidd_void();
        }

        protected void btndel_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_zj_del";
            tab = "vi_zj_del";
            Grid1.Title = "回收站信息";

            BindGrid(); show_void();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {


            DataTable table;

            table = BLL.Bllbase.get_table(tab, whe);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

 


        #region Events

        protected void btnGet_Click(object sender, EventArgs e)
        {
            if (tab != "vi_zj_pub" & tab !=" vi_zj_del")
            {
                Alert.ShowInTop("请在公共信息或回收站使用此功能");
                return;
            }
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row+val][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了收藏项：" + sb.ToString().TrimEnd(','));
            //HY.BLL.Bllbase.DeleteList(tab,sb.ToString().TrimEnd(','));

            //写入收藏表
            Alert.Show(BLL.BllSales.getsales("中介", sb.ToString().TrimEnd(','), ubh, um,"ZJ"));

            BindGrid();
        }


        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
              string key = txt.Text.Trim();
             
              try
              {
                  if (key == "") whe = BLL.pub.pubwhere.get_where_sql(us, tab);
                  BindGrid();

              }catch(Exception ex)
              {
                //  Alert.Show("查询条件错误");
              }
              finally
              {
                  whe = "";
              }
        }

        #endregion
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = txt.Text.Trim();
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
              if (ddlf=="sid"||ddlf=="编号") 
                  whe = "" + ddlf + " " + ddlw + " " + key + "";
              else
                  whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }

      
    

        }




    
}